package com.weimob.mcloud.wechatwork.customer.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.weimob.artemis.client.consumer.model.ConsumerRecord;
import com.weimob.artemis.client.consumer.model.MessageResult;
import com.weimob.artemis.client.spring.annotation.ArtemisConsumerListener;
import com.weimob.artemis.client.spring.annotation.Subscribe;
import com.weimob.mcloud.wechatwork.core.adapter.service.ChannelAdapterService;
import com.weimob.mcloud.wechatwork.customer.domain.ArtemisConstant;
import com.weimob.mcloud.wechatwork.customer.domain.BosChannelChangeMsg;
import com.weimob.mcloud.wechatwork.customer.domain.enums.BosChannelChangeEnum;
import com.weimob.mcloud.wechatwork.customer.domain.enums.BosChannelTypeEnum;
import com.weimob.mcloud.wechatwork.customer.domain.enums.SuiteTypeEnum;
import com.weimob.mcloud.wechatwork.customer.service.TagManageService;
import com.weimob.mcloud.wechatwork.customer.third.PrivateSphereThird;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 渠道绑定解绑
 */
@Component
@Slf4j
public class CorpwxChannelChangeConsumer {

    @Autowired
    private ChannelAdapterService channelAdapterService;

    @Autowired
    private TagManageService tagManageService;

    @Autowired
    private PrivateSphereThird privateSphereThird;

    /**
     * 渠道绑定解绑 http://gitbook.internal.weimob.com/read/spf-bc-book/date-2020.12.04.11.20.40
     */
    @ArtemisConsumerListener(
            value = @Subscribe(topic = ArtemisConstant.Topic.WECHATWORK_CHANNEL_BIND_MSG, user = ArtemisConstant.USER), interval = 2000)
    public void initConsumer(MessageResult result) {

        // 1.取出消息中的全部：企业微信绑定/解绑回调
        List<ConsumerRecord> records = result.getRecords();


        // 2.循环处理每个回调
        for (ConsumerRecord consumerRecord : records) {

            log.info("消费实时企业微信绑定解绑 message：{}", JSON.toJSONString(consumerRecord));


            try {
                // 3.回调内容object -> json String -> class instance
                String value = consumerRecord.getValue().toString();
                BosChannelChangeMsg bosChannelChangeMsg =
                        JSONObject.parseObject(value, BosChannelChangeMsg.class);


                // 4、判断渠道类型
                if (BosChannelTypeEnum.WECHATWORK.getCode().equals(bosChannelChangeMsg.getChannel())) {


                    // 4.1、获取corpId
                    if (SuiteTypeEnum.CONTACT_APP.getCode().equals(bosChannelChangeMsg.getSuiteType())) {
                        //外部联系人授权
                        if (BosChannelChangeEnum.BIND.getValue().equals(bosChannelChangeMsg.getNoticeType())) {
                            //应用绑定
                            //todo 同步历史外部历史联系人
//                            ExternalContactSyncDTO externalContactSyncDTO = new ExternalContactSyncDTO();
//                            externalContactSyncDTO.setCorpId(bosChannelChangeMsg.getCorpId());
//                            externalContactSyncDTO.setSuiteId(bosChannelChangeMsg.getSuiteId());
//                            channelAdapterService.syncCorpWxAllExternalContact(externalContactSyncDTO);
                        }


                    } else if (SuiteTypeEnum.SELF_APP.getCode().equals(bosChannelChangeMsg.getSuiteType())) {

                        //todo 自建应用

                    } else if (SuiteTypeEnum.ADDRESS_BOOK_APP.getCode().equals(bosChannelChangeMsg.getSuiteType())) {

                        //todo 通讯录应用

                    } else if (SuiteTypeEnum.PLATFROM_APP.getCode().equals(Integer.valueOf(bosChannelChangeMsg.getSuiteType()))) {

//                        if (bosChannelChangeMsg.getNoticeType().equals(BosChannelChangeEnum.BIND.getValue())) {
//
//                            //绑定
//                            try {
//
//                                log.info("第三方应用授权初始化标签, corpId:{}", bosChannelChangeMsg.getCorpId());
//                                //初始化标签
//                                tagManageService.initTag(bosChannelChangeMsg.getCorpId());
//                            } catch (Exception e) {
//                                log.error("第三方应用授权初始化标签失败, corpId:{}, error:{}", bosChannelChangeMsg.getCorpId(), JSON.toJSONString(e));
//                            }
//                        }


                    }
                }
            } catch (Exception e) {
                log.error("消费实时企业微信绑定解绑 Artemis数据处理失败. error:{}", e);
            }
        }
    }

}
